<?php
namespace app\admin\controller;

use app\admin\model\ClassroomModel;
use app\admin\Model\MajorModel;
use app\admin\Model\PlistModel;
use app\admin\model\StudentModel;
use cmf\controller\AdminBaseController;
use think\db\Where;
use think\Validate;
use app\admin\Model\School as SchoolModel;
class StudentController extends AdminBaseController {
    public function index() {
        extract(input());
        $where = new Where;
        if(isset($name) && !empty($name)){
            $where['name'] = ['like','%'.$name.'%'];
        }
        if (isset($school_id) && !empty($school_id)) {
            $where['school_id'] = $school_id;
            // 如果 school_id 不为空，说明本次搜索选择了学校。
            // 既然知道了学校，就可以将该学校下的专业查询出来，循环输出在搜索下拉框中，如此，就可以在搜索完成时选中选择的专业了。
            $class = MajorModel::where('school_id', $school_id)->select();
        } else {
            // 本次没有选择学校进行搜索，用一个空数组代替。
            $class = [];
        }
        if (isset($major_id) && !empty($major_id)) {
            $where['major_id'] = $major_id;
            $room = ClassroomModel::where('major_id', $major_id)->select();
        } else {
            // 本次没有选择学校进行搜索，用一个空数组代替。
            $room = [];
        }
        if (isset($class_id) && !empty($class_id)) {
            $where['class_id'] = $class_id;
        }
        $data = StudentModel::with(['classroom.major.school'])
            ->where($where)
            ->with('Plist')
            ->order('id Desc')
            ->paginate(3,false,['query'=>input()]);
        $school = SchoolModel::all();
        $page = $data->render();
        return $this->fetch('',[
            'data' => $data,
            'page' => $page,
            'school'=>$school,
            'class' => $class,
            'classes' =>$room
        ]);
    }
    public function add(){
        $data = SchoolModel::all();
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function Get_major()
    {
        $schoolId = input('school_id');
        $major = MajorModel::where('school_id', $schoolId)
            ->select();
        $options = ["<option value=''>请选择专业</option>"];
        foreach ($major as $v) {
            $options[] = "<option value='" . $v['id'] . "'>" . $v['major_name'] . "</option>";
        }
        return $options;
    }

    public function Get_class()
    {
        $majorID = input('major_id');
        $major = ClassroomModel::where('major_id', $majorID)
            ->select();
        $options = ["<option value=''>请选择班级</option>"];
        foreach ($major as $v) {
            $options[] = "<option value='" . $v['id'] . "'>" . $v['class_name'] . "</option>";
        }
        return $options;
    }
    public function addPost(){
        $data = input ();
        $add = StudentModel::create($data);
        if ($add) {
            return json(['code'=>1,'msg'=>'添加成功']);
        }else{
            return json(['code'=>2,'msg'=>'添加失败']);
        }
    }
    public function edit(){
        $id = input('id');
        $student = StudentModel::find($id);
        $school = SchoolModel::all();
        // 找到该学生所在专业 ID
        $majorId = ClassroomModel::where('id', $student['class_id'])->value('major_id');
        // 找到该学生所在学校的 ID
        $schoolId = MajorModel::where('id', $majorId)->value('school_id');
        // 查询该学生所在学校下的专业
        $major = MajorModel::where('school_id', $schoolId)->select();
        // 查询该学生所在专业下的班级
        $classroom = ClassroomModel::where('major_id', $majorId)->select();
        return $this->fetch('edit', [
            'data' => $student,
            'school' => $school,
            'majorId' => $majorId,
            'schoolId' => $schoolId,
            'major' =>$major,
            'classroom' => $classroom
        ]);
    }
    public function editPost(){
        $data = input();
        $rule = [
            'name'  => 'require',
            'sex' => 'require|integer',
            'age' => 'require|integer',
            'idcard' => 'require|max:18',
            'address' => 'require',

        ];

        $msg = [
            'name.require' => '姓名不能为空',
            'sex.require' => '性别不能为空',
            'sex.integer' => '性别传值错误',
            'age.require' => '年龄不能为空',
            'age.integer' => '年龄传值错误',
            'idcard.require' => '身份证号不能为空',
            'idcard.max' => '身份证号不能超过18位',
            'address.require' => '家庭住址不能为空',

        ];
        $validate = new Validate($rule, $msg);
        $result  = $validate->check($data);
        if (true !== $result) {
            $this->error($validate->getError());
        }
        $edit = StudentModel::where('id',$data['id'])->update($data);
        if($edit){
            return json(['code'=>1,'msg'=>'修改成功']);
        }else{
            return json(['code'=>2,'msg'=>'修改失败']);
        }
    }
    public function delete(){
        $id = input('id');
        $del  = StudentModel::destroy($id);
        if($del){
            return json(['code'=>1,'msg'=>'删除成功']);
        }else{
            return json(['code'=>2,'msg'=>'删除失败']);
        }
    }
    public function delAll() {
        $ids = input('ids/a');
        $del = StudentModel::destroy($ids);
        if($del){
            $this->success('删除成功','student/index');
        }else{
            $this->error('删除失败','student/index');
        }
    }
}